CREATE PROCEDURE dbo.ds_insert_file
(
	@FileName	VARCHAR(255), 
	@ContentLength	INT, 
	@MimeType	VARCHAR(255), 
	@FileStream	IMAGE
)
AS
BEGIN
	INSERT INTO FileStorage 
	(FileName, ContentLength, MimeType, FileStream) 
	VALUES 
	(@FileName, @ContentLength, @MimeType, @FileStream)
END
GO
GRANT  EXECUTE  ON [dbo].[ds_insert_file]  TO [webuser]
GO

CREATE PROCEDURE dbo.ds_update_file
(
	@FileName	VARCHAR(255), 
	@ContentLength	INT, 
	@MimeType	VARCHAR(255), 
	@FileStream	IMAGE
)
AS
BEGIN
	UPDATE 	FileStorage 
	SET 	FileStream = @FileStream, 
		ContentLength = @ContentLength, 
		MimeType = @MimeType 
	WHERE 	FileName = @FileName
END
GO
GRANT  EXECUTE  ON [dbo].[ds_update_file]  TO [webuser]
GO

CREATE PROCEDURE dbo.ds_get_file
(
	@FileName	VARCHAR(255)
)
AS
BEGIN
	SELECT 	FileName, 
		ContentLength, 
		MimeType, 
		FileStream 
	FROM 	FileStorage 
	WHERE 	FileName = @FileName
END
GO
GRANT  EXECUTE  ON [dbo].[ds_get_file]  TO [webuser]
GO


CREATE PROCEDURE dbo.ds_get_file_list
AS
BEGIN
	SELECT 	FileName 
	FROM 	FileStorage
END
GO
GRANT  EXECUTE  ON [dbo].[ds_get_file_list]  TO [webuser]
GO

CREATE PROCEDURE dbo.ds_delete_file
(
	@FileName	VARCHAR(255)
)
AS
BEGIN
	DELETE 	FileStorage 
	WHERE 	FileName = @FileName
END
GO
GRANT  EXECUTE  ON [dbo].[ds_delete_file]  TO [webuser]
GO

CREATE PROCEDURE dbo.ds_get_count
(
	@FileName	VARCHAR(255)
)
AS
BEGIN
	SELECT 	COUNT(*) 
	FROM 	FileStorage 
	WHERE 	FileName = @FileName
END
GO
GRANT  EXECUTE  ON [dbo].[ds_get_count]  TO [webuser]
GO

CREATE PROCEDURE dbo.ds_copy_file
(
	@DestFileName 	VARCHAR(255), 
	@SourceFileName	VARCHAR(255)
)
AS
BEGIN
	INSERT INTO FileStorage 
	SELECT 	@DestFileName AS FileName, 
		ContentLength, 
		MimeType, 
		FileStream 
	FROM 	FileStorage 
	WHERE 	FileName = @SourceFileName
END
GO
GRANT  EXECUTE  ON [dbo].[ds_copy_file]  TO [webuser]
GO
